package com.zhongfl.crm.service.impl.boss;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhongfl.crm.annotation.FieldMeta;
import com.zhongfl.crm.enums.boss.CommonLogType;
import com.zhongfl.crm.lixiao.mapper.CommonLogMapper;
import com.zhongfl.crm.service.boss.CommonLogAtomService;
import com.zhongfl.crm.service.boss.CommonLogService;
import com.zhongfl.crm.model.boss.CommonLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tech.heptagram.core.common.util.JsonUtil;

import javax.annotation.Resource;
import java.beans.IntrospectionException;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class CommonLogServiceImpl implements CommonLogService {

    @Resource
    private CommonLogMapper commonLogMapper;

    @Resource
    private CommonLogAtomService commonLogAtomService;

    @Override
    public void add(CommonLogType type, CommonLogType typeExt,
                    String key, Object source,
                    Object target,
                    String user) throws JsonProcessingException, IntrospectionException {
        add(type, typeExt, key, source, target, user);
    }

    @Override
    public void addWithFieldMeta(CommonLogType type, CommonLogType typeExt, String key, Object source, Object target, String user) throws JsonProcessingException, IntrospectionException {
        commonLogAtomService.add(type, typeExt, key, source, target, user, FieldMeta.class);
    }

    @Override
    public void add(CommonLogType type, CommonLogType typeExt,
                    String key, Object source, String user) throws JsonProcessingException, IntrospectionException {
        CommonLog log = new CommonLog();
        log.setType(type);
        log.setTypeExt(typeExt);
        log.setKey(key);
        if (source instanceof String) {
            log.setMessage(source.toString());
        } else {
            log.setMessage("对象初始化");
            log.setAfter(JsonUtil.toJson(source));
        }
        log.setCreatedBy(user);
        commonLogMapper.insert(log);
    }

    @Override
    public PageInfo<CommonLog> findForPage(Integer page, Integer pageSize, Integer type, String key) {
        PageHelper.startPage(page, pageSize);
        List<CommonLog> pageInfo = commonLogMapper.selectByType(type.toString(), key);
        return new PageInfo<>(pageInfo);
    }

    @Override
    public PageInfo<CommonLog> findForPage(Integer page, Integer pageSize, Integer type, Integer typeExt, String key) {
        PageHelper.startPage(page, pageSize);
        List<CommonLog> pageInfo = commonLogMapper.selectByTypeAndTypeExt(type.toString(), typeExt.toString(), key);
        return new PageInfo<>(pageInfo);
    }

    /***
     * 根据订单号查询订单日志
     * @param key
     * @return
     */
    @Override
    public List<CommonLog> findOrderLog(String key) {
        return findByKeyAndType(key, CommonLogType.ORDER.toString());
    }

    @Override
    public List<CommonLog> findByKeyAndType(String key, String type) {
        List<CommonLog> commonLogs = commonLogMapper.selectByType(type, key);
        for (CommonLog commonLog : commonLogs) {
            if (StringUtils.isEmpty(commonLog.getMessage())) {
                continue;
            }
            String[] logs = StringUtils.split(commonLog.getMessage(), "\n");
            commonLog.setMessages(Arrays.asList(logs));
        }
        return commonLogs;
    }

    @Override
    public List<CommonLog> findListByKeyAndType(String key, String typeExt) {
        List<CommonLog> commonLogs = commonLogMapper.selectListByTypeExtAndKey(typeExt, key);
        return commonLogs;
    }

    @Override
    public List<CommonLog> findByTypeExtAndKey(String key, String typeExt) {
        return commonLogMapper.selectByTypeExtAndKey(key, typeExt);
    }
}
